package com.navinfo.opentsp.platform.computing.service.huashen;

import com.navinfo.opentsp.platform.computing.anno.RoutingProtocol;
import com.navinfo.opentsp.platform.computing.enums.ProtocolEnum;
import com.navinfo.opentsp.platform.computing.enums.YardEnum;
import com.navinfo.opentsp.platform.computing.service.IProtocolParseService;
import com.navinfo.opentsp.platform.computing.util.DateUtils;
import com.navinfo.opentsp.platform.location.protocol.gbv6.LCPlatformAnswerOriginalMessage;
import org.apache.flume.Event;

@RoutingProtocol(yard = YardEnum.HUASHEN, protocolType = ProtocolEnum.SIX_MESSAGE_ANSWER)
public class Cn6AnswerOriginalMessageService implements IProtocolParseService {
    @Override
    public String parseProtocol(Event event) {
        try {
            //拼写落盘到HDFS的字符串。字段间分隔符是\001，null通过\N表示
            LCPlatformAnswerOriginalMessage.PlatformAnswerOriginalMessage answerOriginalMessage = LCPlatformAnswerOriginalMessage.PlatformAnswerOriginalMessage.parseFrom(event.getBody());

            StringBuilder answerOriginalMessageStr = new StringBuilder(10000);
            answerOriginalMessageStr
                    .append(answerOriginalMessage.getOriginal()).append(splitStr)
                    .append(answerOriginalMessage.getGpsTime()).append(splitStr)
                    .append(answerOriginalMessage.getVin()).append(splitStr)
                    .append(answerOriginalMessage.getAnswerId()).append(splitStr)

                    //添加字段只能在分割线之上添加, 注意时间精度是秒还是毫秒
                    //------------------------------------------分割线----------------------------------------
                    .append(DateUtils.format(answerOriginalMessage.getGpsTime(), "yyyyMMdd")).append(splitStr);
            return answerOriginalMessageStr.toString();
        } catch (Exception e) {
            throw new RuntimeException("执行HDFS的HDFSObdInformationSerializer拦截器失败：" + e.getMessage(), e);
        }
    }
}
